package com.yt.base.controller.app;

import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import com.yt.app.core.interfaces.ActivityService;
import com.yt.app.core.model.Activity;
import com.yt.base.controller.core.BaseController;
import com.yt.core.MsgBean;
import com.yt.core.dao.PagingBean;
import com.yt.core.dao.filter.DynamicSQLFilterImpl;
import com.yt.core.dao.filter.QueryFilter;
import com.yt.util.YitianConfig;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/app/activity")
public class ActivityController extends BaseController {

    @Autowired
    private ActivityService activityService;

    @RequestMapping("list")
    public ModelAndView list(@RequestParam(value = "start", required = false) String start,
                             @RequestParam(value = "limit", required = false) String limit) {
        ModelAndView mav = new ModelAndView("/app/activity/list");
        QueryFilter filter = new DynamicSQLFilterImpl();
        filter.setSortField("id", true);
        PagingBean page = new PagingBean();
        int currentPage = 0;
        int pageSize = 2;
        if (StringUtils.isNotEmpty(limit) && StringUtils.isNumeric(limit)) {
            pageSize = Integer.parseInt(limit);
        }
        if (StringUtils.isNotEmpty(start) && StringUtils.isNumeric(start)) {
            currentPage = (Integer.parseInt(start) - 1) * pageSize;
        }
        page.setPageSize(pageSize);
        page.setStart(currentPage);

        List<Activity> list = activityService.find(filter);

        int total = activityService.count(filter);
        mav.addObject("total", total);
        mav.addObject("list", list);
        return mav;
    }

    @RequestMapping("toAdd")
    public ModelAndView toAdd() {
        ModelAndView mav = new ModelAndView("/app/activity/add");
        return mav;
    }


    @RequestMapping("toEdit")
    public ModelAndView toEdit(@RequestParam(value = "id", required = true) Long id) {
        ModelAndView mav = new ModelAndView("/app/activity/add");
        Activity activity = activityService.get(id);
        mav.addObject("obj", activity);
        return mav;
    }

    @RequestMapping("save")
    public
    @ResponseBody
    Object save(@RequestParam(value = "id", required = false) Long id,
                @RequestParam(value = "title", required = false) String title,
                @RequestParam(value = "image", required = false) String image,
                @RequestParam(value = "link", required = false) String link,
                @RequestParam(value = "startDate", required = false) String startDateText,
                @RequestParam(value = "endDate", required = false) String endDateText) {
        MsgBean msg = new MsgBean();
        try {
            Activity activity = null;
            if (id != null && id > 0) {
                activity = activityService.get(id);
            }

            if (activity == null) {
                activity = new Activity();
                activity.setStatus(0);
                activity.setUserCount(0);
            }
            if (Strings.isNullOrEmpty(title)) {
                msg.setFailure(1, "标题不能为空！");
                return msg.returnMsg();
            } else if (title.length() > 20) {
                msg.setFailure(1, "标题不能超过20个字符！");
                return msg.returnMsg();
            }
            activity.setTitle(title.trim());

            if (Strings.isNullOrEmpty(image)) {
                msg.setFailure(1, "请上传图片！");
                return msg.returnMsg();
            }
            List<String> imageList = Splitter.on(",").splitToList(image);
            if (imageList == null || imageList.size() == 0) {
                msg.setFailure(1, "请上传图片！");
                return msg.returnMsg();
            }
            image = imageList.get(0);
            if (image.startsWith(YitianConfig.IMAGE_ACCESS_URL)) {
                activity.setImage(image);
            } else {
                activity.setImage(YitianConfig.IMAGE_ACCESS_URL + "/" + image);
            }
            if (Strings.isNullOrEmpty(link)) {
                msg.setFailure(1, "跳转链接不能为空！");
                return msg.returnMsg();
            }
            activity.setLink(link.trim());

            if (Strings.isNullOrEmpty(startDateText)) {
                msg.setFailure(1, "请输入开始时间！");
                return msg.returnMsg();
            }
            if (Strings.isNullOrEmpty(endDateText)) {
                msg.setFailure(1, "请输入结束时间！");
                return msg.returnMsg();
            }

            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date startDate = dateFormat.parse(startDateText);
            Date endDate = dateFormat.parse(endDateText);

            activity.setStartDate(startDate.getTime() / 1000);
            activity.setEndDate(endDate.getTime() / 1000);

            if (activity.getId() != null && activity.getId() > 0) {
                activityService.update(activity);
            } else {
                activityService.insert(activity);
            }
        } catch (Exception e) {
            msg.failure("操作失败，请检查参数是否合理！");
        }
        return msg.returnMsg();
    }

    @RequestMapping("/multiDel")
    @ResponseBody
    public Object multiDel(@RequestParam("ids") String ids) {
        MsgBean msg = new MsgBean();
        String[] idArr = ids.split(",");
        if (ids != null) {
            for (String id : idArr) {
                activityService.remove(Long.valueOf(id));
            }
        }
        return msg.returnMsg();
    }

    @RequestMapping("/updateStatus")
    @ResponseBody
    public Object updateStatus(@RequestParam("id") Long id, @RequestParam("status") Integer status) {
        MsgBean msg = new MsgBean();
        Activity activity = activityService.get(id);
        if (activity == null) {
            msg.setFailure(1, "该活动不存在！");
            return msg.returnMsg();
        }
        activity.setStatus(status);
        activityService.update(activity);
        return msg.returnMsg();
    }


}
